"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const mes_1 = __importDefault(require("../utils/mes"));
const crypto_1 = __importDefault(require("../utils/crypto"));
const jwt_1 = require("../utils/jwt");
class User {
}
User.reg = (req, res) => {
    try {
        const { name, pass } = req.body;
        const userList = JSON.parse(fs_1.default.readFileSync('./src/controller/userList.json', 'utf8'));
        const doc = userList.find((item) => item.name === name);
        if (doc)
            throw '该用户已被注册';
        const userInfo = {
            id: Date.now(),
            name,
            pass: (0, crypto_1.default)(pass)
        };
        userList.push(userInfo);
        fs_1.default.writeFileSync('./src/controller/userList.json', JSON.stringify(userList), 'utf8');
        res.send(mes_1.default.success('注册成功'));
    }
    catch (error) {
        res.status(500).send(mes_1.default.failed(error));
    }
};
User.login = (req, res) => {
    try {
        const { name, pass } = req.body;
        const userList = JSON.parse(fs_1.default.readFileSync('./src/controller/userList.json', 'utf8'));
        const doc = userList.find((item) => item.name === name);
        if (!doc)
            throw '该用户名不存在';
        if ((0, crypto_1.default)(pass) !== doc.pass)
            throw '用户名或密码不正确';
        const token = (0, jwt_1.generateToken)(doc);
        res.send(mes_1.default.success('登录成功，欢迎回来', { userInfo: doc, token }));
    }
    catch (error) {
        res.status(500).send(mes_1.default.failed(error));
    }
};
User.userList = (req, res) => {
    try {
        const { limit = 10, offset = 0 } = req.query;
        const userList = JSON.parse(fs_1.default.readFileSync('./src/controller/userList.json', 'utf8'));
        const list = userList.slice(Number(offset), Number(offset) + Number(limit));
        console.log(list);
        res.send(mes_1.default.success('用户列表', { userList: list }));
    }
    catch (error) {
        res.status(500).send(mes_1.default.failed(error));
    }
};
User.update = (req, res) => {
    try {
        const { name, newName, newPass } = req.body;
        const userList = JSON.parse(fs_1.default.readFileSync('./src/controller/userList.json', 'utf8'));
        const doc = userList.findIndex((item) => item.name === name);
        if (!~doc)
            throw '该用户不存在或已被删除';
        const info = userList[doc];
        const cryptoPass = newPass ? (0, crypto_1.default)(newPass) : info.pass;
        const userInfo = {
            id: info.id,
            name: newName !== null && newName !== void 0 ? newName : info.name,
            pass: cryptoPass
        };
        userList[doc] = userInfo;
        fs_1.default.writeFileSync('./src/controller/userList.json', JSON.stringify(userList), 'utf8');
        res.send(mes_1.default.success('更新成功', { userInfo }));
    }
    catch (error) {
        res.status(500).send(mes_1.default.failed(error));
    }
};
User.delete = (req, res) => {
    try {
        const { name } = req.body;
        const userList = JSON.parse(fs_1.default.readFileSync('./src/controller/userList.json', 'utf8'));
        const doc = userList.findIndex((item) => item.name === name);
        if (!~doc)
            throw '该用户不存在或已被删除';
        userList.splice(doc, 1);
        fs_1.default.writeFileSync('./src/controller/userList.json', JSON.stringify(userList), 'utf8');
        res.send(mes_1.default.success('删除成功'));
    }
    catch (error) {
        res.status(500).send(mes_1.default.failed(error));
    }
};
exports.default = User;
